﻿/// <reference path="typings/jquery/jquery.d.ts" />
/// <reference path="typings/knockout.mapping/knockout.mapping.d.ts" />

module Ecatalog.Api {

    export class GetComplectsParams {
        OrganizationId: number;
        NumModel: string;
    }

    export class LocationDto {
        ModelId: number;
        CatNumber: string;
        CoordinateX: number;
        CoordinateY: number;
    }

    export class ComplectsDto {
        ActiveModelId: number;
        KitDtos: KitDto[];
        ActiveKitId: number;
        ShopMapUrl: string;
        ShopId: number;
    }

    export class KitDto {
        CostComplect: number;
        DiscountComplect: number;
        ImageUrl: string;
        ImageUrlFull: string;
        KitId: number;
        ModelDtos: ModelDto[];
        LocationDtos: LocationDto[];
    }

    export class ModelDto {
        ModelId: number;
        ImageUrl: string;
        StructuresDtos: StructuresDto[];
        ColorsDtos: ColorsDto[];
        SizeDtos: SizeDto[];
        KitIds: number[];
        Description: string;    
        LocationDto: LocationDto;    
        SalePrice: number;
        Sale: number;
    }

    export class StructuresDto {
        Name: string;
        Value: string;
    }

    export class ColorsDto {
        RgbNumber: string;
        Name: string;
    }

    export class SizeDto {
        ModelId: number;
        NumModel: string;
        Value: number;
        Exist: boolean;
    }

    export class DataLoader {
        load: (data: any, success: (result: any) => void) => void;
        constructor(url: string, httpMethod: string) {
            this.load = (data, success) => {
                var jsonData = data == null ? null : ko.mapping.toJSON(data);
                var settings: JQueryAjaxSettings = {
                    url: url,
                    dataType: "json",
                    type: httpMethod,
                    data: jsonData,
                    contentType: "application/json; charset=utf-8",
                    error: () => { alert('При запросе данных произошла ошибка'); },
                    beforeSend: function () { },
                    success: function (data) {
                        success(data);
                    },
                    complete: function () { }
                }
				jQuery.ajax(settings);
            }
		}
    }

    export class AsyncClient {
        static Current = new AsyncClient();

        GetComplects = (data: GetComplectsParams, success: (result: ComplectsDto) => void) => {
            var dataLoader = new DataLoader("/base/GetComplectsJson", "Post");
            dataLoader.load(data, (result) => {
                success(result);
            });
        }
   }
}